package org.jash.mvp2206.video;

import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import org.jash.mvp2206.R;
import org.jash.mvp2206.entity.AllVideo;

import java.util.ArrayList;
import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
    private List<AllVideo.DataBean> data = new ArrayList<>();
    private OnVideoClickListener listener;
    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VideoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        AllVideo.DataBean bean = data.get(position);
        holder.title.setText(bean.getTitle());
        Glide.with(holder.img).load(bean.getImgurl()).into(holder.img);
        if (listener != null) {
            holder.itemView.setOnClickListener(v -> listener.onClick(bean));
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    public void addAll(List<AllVideo.DataBean> list) {
        int size = data.size();
        data.addAll(list);
        notifyItemRangeInserted(size, list.size());
    }

    public void setListener(OnVideoClickListener listener) {
        this.listener = listener;
    }

    public interface OnVideoClickListener {
        void onClick(AllVideo.DataBean bean);
    }
    public static class VideoViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;
        private TextView title;
        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            title = itemView.findViewById(R.id.title);
        }
    }
}
